{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e507dd6",
   "metadata": {},
   "source": [
    "# Bigrams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fb2bc1d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run -i \"../util/util_simple_classifier.ipynb\"\n",
    "from sklearn.feature_extraction.text import CountVectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "93a33879",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Found cached dataset rotten_tomatoes (/home/zhenya/.cache/huggingface/datasets/rotten_tomatoes/default/1.0.0/40d411e45a6ce3484deed7cc15b82a53dad9a72aafd9f86f8f227134bec5ca46)\n",
      "Found cached dataset rotten_tomatoes (/home/zhenya/.cache/huggingface/datasets/rotten_tomatoes/default/1.0.0/40d411e45a6ce3484deed7cc15b82a53dad9a72aafd9f86f8f227134bec5ca46)\n"
     ]
    }
   ],
   "source": [
    "(train_df, test_df) = load_train_test_dataset_pd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "eb378a6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "bigram_vectorizer = CountVectorizer(ngram_range=(1, 2), max_df=300)\n",
    "X = bigram_vectorizer.fit_transform(train_df[\"text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bc2ec1c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['10' '10 inch' '10 set' ... 'ótimo esforço' 'últimos' 'últimos tiempos']\n",
      "40535\n"
     ]
    }
   ],
   "source": [
    "print(bigram_vectorizer.get_feature_names_out())\n",
    "print(len(bigram_vectorizer.get_feature_names_out()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6d51ad30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 ... 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "first_review = test_df['text'].iat[0]\n",
    "dense_vector = bigram_vectorizer.transform([first_review]).todense()\n",
    "print(dense_vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1ece4026",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.75      0.72      0.73       160\n",
      "           1       0.73      0.76      0.74       160\n",
      "\n",
      "    accuracy                           0.74       320\n",
      "   macro avg       0.74      0.74      0.74       320\n",
      "weighted avg       0.74      0.74      0.74       320\n",
      "\n"
     ]
    }
   ],
   "source": [
    "vectorize = lambda x: bigram_vectorizer.transform([x]).toarray()[0]\n",
    "(X_train, X_test, y_train, y_test) = create_train_test_data(train_df, test_df, vectorize)\n",
    "clf = train_classifier(X_train, y_train)\n",
    "test_classifier(test_df, clf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62a3dce6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
